package cn.zm.trip.web.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSONUtil;
import cn.zm.trip.web.commons.Msg;
import cn.zm.trip.web.dao.HotelDao;
import cn.zm.trip.web.dao.ViewPointDao;
import cn.zm.trip.web.domain.*;
import cn.zm.trip.web.service.*;
import cn.zm.trip.web.utils.BeanCopyUtils;
import cn.zm.trip.web.vo.FoodVo;
import cn.zm.trip.web.vo.HotelVO;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 投诉与反馈控制器
 *
 * @author chen
 * @date 2024/03/19
 */
@Controller
@RequiredArgsConstructor
@RequestMapping(value = "feedback")
public class FeedbackController {

    private final FeedbackService feedbackService;
    private final ViewPointService viewPointService;
    private final HotelDao hotelDao;
    private final HotelFacilityService hotelFacilityService;
    private final WordsService wordsService;

    private final FoodService foodService;

    /**
     * 跳转首页
     */
    @GetMapping("/index")
    public String index(){
        // 返回视图路径，用于投诉列表页面
        return "proscenium/feedback/index";
    }

    /**
     * content
     */
    @PostMapping("/saveHotel")
    public String saveHotel(Feedback feedback,String hotelIdStr, Model model){

        if (CharSequenceUtil.isNotEmpty(hotelIdStr)){
            feedback.setHotelId(Integer.valueOf(hotelIdStr));
        }
        feedbackService.save(feedback);
        //封装留言信息
        List<Words> lw_list = wordsService.findByWords();
        model.addAttribute("lw_list",lw_list);

        //封装回复信息
        List<Reply> lr_list = wordsService.findByReply();
        model.addAttribute("lr_list",lr_list);

        Hotel hotel = hotelDao.selectByPrimaryKey(Integer.valueOf(hotelIdStr));
        HotelFacility hotelFacility = hotelFacilityService
                .getOne(new LambdaQueryWrapper<HotelFacility>().eq(HotelFacility::getHotelId, hotel.getHid()));
        model.addAttribute("hotel", HotelVO.buildHotelVO(hotel,hotelFacility));

        return "proscenium/hotel/content";

    }

    @PostMapping("/saveFood")
    public String saveFood(Feedback feedback,String foodIdStr, Model model){
        if (CharSequenceUtil.isNotEmpty(foodIdStr)){
            // 剪切掉最后一个字符（即最后一个斜杠）不知道为什么带斜杠了（又不带了）
            feedback.setFoodId(Integer.valueOf(foodIdStr));
        }
        feedbackService.save(feedback);
        //封装留言信息
        List<Words> lw_list = wordsService.findByWords();
        model.addAttribute("lw_list",lw_list);

        //封装回复信息
        List<Reply> lr_list = wordsService.findByReply();
        model.addAttribute("lr_list",lr_list);
        // 从食品服务获取食品
        Food food = foodService.getById(Integer.valueOf(foodIdStr));
        // 将食品列表添加到模型中，以便在前端页面中访问
        FoodVo foodVo = BeanCopyUtils.copyBean(food, FoodVo.class);
        if (CharSequenceUtil.isNotEmpty(foodVo.getPictureUrls())){
            foodVo.setPictureLists(JSONUtil.toBean(food.getPictureUrls(), new TypeReference<List<String>>(){},true));
        }
        model.addAttribute("food", foodVo);
        // 返回视图路径，用于渲染食品列表页面
        return "proscenium/food/content";
    }
    @PostMapping("/saveViewpoint")
    public String saveViewpoint(Feedback feedback,String viewpointIdStr, Model model){
        if (CharSequenceUtil.isNotEmpty(viewpointIdStr)){
            feedback.setViewpointId(Integer.valueOf(viewpointIdStr));
        }
        feedbackService.save(feedback);

        //封装留言信息
        List<Words> lw_list = wordsService.findByWords();
        model.addAttribute("lw_list",lw_list);

        //封装回复信息
        List<Reply> lr_list = wordsService.findByReply();
        model.addAttribute("lr_list",lr_list);

        //留言统计接口
        List<WordStatisticsEntity> wordStatisticsEntities = wordsService.listWordStaticsByWordQuery(Integer.valueOf(viewpointIdStr), null,null);
        if (CollUtil.isNotEmpty(wordStatisticsEntities)) {
            int sum = wordStatisticsEntities.stream().mapToInt(WordStatisticsEntity::getQuantity).sum();

            for (WordStatisticsEntity statisticsEntity : wordStatisticsEntities) {
                Integer quantity = statisticsEntity.getQuantity();
                double radio = (quantity * 1.0) / sum;
                String proportion = (int) ((radio) * 100) + "%";
                statisticsEntity.setScoreTextByScore();
                statisticsEntity.setScoreRatioText(proportion);
            }
            model.addAttribute("wordStatics",wordStatisticsEntities);
        }

        ViewPoint viewPoint = viewPointService.selectByPrimaryKey(Integer.valueOf(viewpointIdStr));
        viewPoint.setPictureList(JSON.parseArray(viewPoint.getPictureUrls(),String.class));
        model.addAttribute("viewPoint", viewPoint);

        return "proscenium/viewpoint/content";
    }
}
